<?php

namespace App\Model;

use App\Exceptions\BizException;

class Sms
{
    const SMS_URL = 'http://106.ihuyi.cn/webservice/sms.php?method=Submit';
    const ACCOUNT = 'cf_yunfanke';
    const PASSWORD = '1234567';

    /**
     * 发送验证码
     *
     * @param $mobile
     * @return string
     */
    public static function sendCheckCode($mobile)
    {
        $mobile_code = Common::codeRandom();
        $content = "您的验证码是：{$mobile_code}。请不要把验证码泄露给其他人。如非本人操作，可不用理会！";
        self::send($mobile, $content);
        return $mobile_code;
    }

    /**
     * 发送短信
     *
     * @param $mobile
     * @param $content
     * @throws BizException
     */
    public static function send($mobile, $content)
    {
        $url = self::SMS_URL;
        $post_data = 'account=' . self::ACCOUNT . '&password=' . self::PASSWORD;
        $post_data .= '&mobile=' . $mobile . "&content=" . rawurlencode($content);
        $gets = Common::xml2array(Common::postCurl($url, $post_data));
        if ($gets['SubmitResult']['code'] != 2)
            throw new BizException('短信发送失败：' . $gets['SubmitResult']['msg']);
    }
}
